package com.open.party.cloud.view.home.juBao

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSpinner
import androidx.core.content.ContextCompat
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.JuBaoBean
import com.open.party.cloud.model.JuBaoVo
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.switchTabView.style1.UIUtil
import kotlinx.android.synthetic.main.activity_12380_ju_bao_ni_ming.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class JuBao12380JuBaoNiMingActivity : AppEventBusTitleBaseActivity() {
    private var class1 = 0
    private var class2 = 0
    var viewModel: BizViewModel? = null
    var area = ""

    override fun getLayoutResId(): Int = R.layout.activity_12380_ju_bao_ni_ming

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("匿名举报")
        class1 = intent.getIntExtra("class1", JuBao12380JuBaoClassActivity.JB_CLass_Ge_Ren)
        class2 = intent.getIntExtra("class2", JuBao12380JuBaoClassActivity.JB_Style_Ni_Ming)
        area = intent.getStringExtra("area") as String

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        if (class1 == JuBao12380JuBaoClassActivity.JB_CLass_Ge_Ren) {
            // 举报个人
            tipTitleTv.text = "被举报人信息"
            input01View.visibility = View.VISIBLE
            input02View.visibility = View.VISIBLE
            input03View.visibility = View.VISIBLE
            input04View.visibility = View.VISIBLE
            input05View.visibility = View.VISIBLE
            input05FlagTv.text = "所在单位："

            input06View.visibility = View.VISIBLE
            input07View.visibility = View.VISIBLE
            input08View.visibility = View.VISIBLE
            input09View.visibility = View.VISIBLE
        } else {
            // 举报单位
            tipTitleTv.text = "被举报单位信息"
            input01View.visibility = View.VISIBLE
            input02View.visibility = View.GONE
            input03View.visibility = View.GONE
            input04View.visibility = View.GONE
            input05View.visibility = View.VISIBLE
            input05FlagTv.text = "被举报单位："

            input06View.visibility = View.GONE
            input07View.visibility = View.VISIBLE
            input08View.visibility = View.VISIBLE
            input09View.visibility = View.VISIBLE
        }

        submitBtn.setOnClickListener {
            submit()
        }

        initSpinnerListView()
    }

    private fun submit() {
        val vo = JuBaoVo()

//        val v01 = areaBeanStr
        val v05 = input05Et.text.toString()
        val v07 = levelStr
        val v08 = questionTypeStr
        val v09 = input09Et.text.toString()

        if (XUtils.string().isEmpty(area)) {
            XUtils.toast().show("所在地区不能为空")
            return
        }
        vo.area = area

        if (XUtils.string().isEmpty(v05)) {
            XUtils.toast().show("请填写所在单位")
            return
        }
        vo.unit = v05

        if (XUtils.string().isEmpty(v07)) {
            XUtils.toast().show("请填写级别")
            return
        }
        vo.level = v07

        if (XUtils.string().isEmpty(v08)) {
            XUtils.toast().show("请填写问题类型")
            return
        }
        vo.questionType = v08

        if (XUtils.string().isEmpty(v09)) {
            XUtils.toast().show("请填写主要问题")
            return
        }
        vo.mainQuestion = v09

        if (class1 == JuBao12380JuBaoClassActivity.JB_CLass_Ge_Ren) {
            vo.type = "user"

            val v02 = input02Et.text.toString()
            if (XUtils.string().isEmpty(v02)) {
                XUtils.toast().show("请填写被举报人")
                return
            }
            vo.name = v02

            val v03 = sexStr
            if (XUtils.string().isEmpty(v03)) {
                XUtils.toast().show("请填写性别")
                return
            }
            vo.sex = v03

            val v04 = input04Et.text.toString()
            if (XUtils.string().isEmpty(v04)) {
                XUtils.toast().show("请填写政治面貌")
                return
            }
            vo.politicalOutlook = v04

            val v06 = input06Et.text.toString()
            if (XUtils.string().isEmpty(v06)) {
                XUtils.toast().show("请填写所任职务")
                return
            }
            vo.post = v06

        } else {
            vo.type = "unit"
        }

        showLoadingDialog("正在提交")
        viewModel!!.putJuBaoInfo(vo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<JuBaoBean>) {
        if (result.eventType != "putJuBaoInfo") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    // 感谢您的支持，我们将尽快核实情况，祝您生活愉快
                    // "code":"1592288194662"

                    CommonDialog.Builder(this)
                        .setTitle("举报已提交")
                        .setCanceledOnTouchOutside(false)
                        .setMessage("举报编码：" + result.data.code + "\n\n感谢您的支持，我们将尽快核实情况，祝您生活愉快!")
                        .setPositiveButton("知道了") {
                            finish()
                        }.setNegativeButton("返回") {
                            finish()
                        }
                        .show()
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().show("Token过期，请重新登录")
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun initSpinnerListView() {
//        initAreaListView()

        input01Et.text = area

        initSexListView()
        initLevelListView()
        initQuestionTypeListView()
    }

//    var areaBeanStr = ""
    var sexStr = ""
    var levelStr = ""
    var questionTypeStr = ""

//    private fun initAreaListView() {
//        val levelBeanList = ArrayList<DictionaryBean>()
//        // {pt:普通;jj:紧急;tj:特急}
//        levelBeanList.add(DictionaryBean("LPSS", "六盘水市"))
//        levelBeanList.add(DictionaryBean("PT", "钟山区"))
//        levelBeanList.add(DictionaryBean("JJ", "六枝特区"))
//        levelBeanList.add(DictionaryBean("TJ", "水城区"))
//        levelBeanList.add(DictionaryBean("TJ", "盘州市"))
//
//        val levelValueList = ArrayList<String>()
//        for (areaBean in levelBeanList) {
//            levelValueList.add(areaBean.desc)
//        }
//
//        initSpinnerView(areaSpinner, levelValueList, object : AdapterView.OnItemSelectedListener {
//            override fun onNothingSelected(parent: AdapterView<*>?) {
//            }
//
//            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//
//                if (view != null) {
//                    val tv = view as TextView
//                    tv.setTextColor(ContextCompat.getColor(this@JuBao12380JuBaoNiMingActivity, R.color.page_content))
//                    tv.paint.textSize = UIUtil.sp2px(this@JuBao12380JuBaoNiMingActivity, 14f).toFloat()
//                    tv.gravity = Gravity.START
//                    areaBeanStr = levelBeanList[position].desc
//                }
//            }
//        })
//    }

    private fun initSexListView() {

        val valueList = JuBaoData.getSexList()

        val valueStrList = ArrayList<String>()
        for (areaBean in valueList) {
            valueStrList.add(areaBean.desc)
        }

        initSpinnerView(sexSpinner, valueStrList, object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

                if (view != null) {
                    val tv = view as TextView
                    tv.setTextColor(ContextCompat.getColor(this@JuBao12380JuBaoNiMingActivity, R.color.page_content))
                    tv.paint.textSize = UIUtil.sp2px(this@JuBao12380JuBaoNiMingActivity, 14f).toFloat()
                    tv.gravity = Gravity.START
                    sexStr = valueList[position].desc
                }
            }
        })
    }

    private fun initLevelListView() {

        val valueList = JuBaoData.getLevelList()

        val valueStrList = ArrayList<String>()
        for (areaBean in valueList) {
            valueStrList.add(areaBean.desc)
        }

        initSpinnerView(levelSpinner, valueStrList, object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

                if (view != null) {
                    val tv = view as TextView
                    tv.setTextColor(ContextCompat.getColor(this@JuBao12380JuBaoNiMingActivity, R.color.page_content))
                    tv.paint.textSize = UIUtil.sp2px(this@JuBao12380JuBaoNiMingActivity, 14f).toFloat()
                    tv.gravity = Gravity.START
                    levelStr = valueList[position].desc
                }
            }
        })
    }

    private fun initQuestionTypeListView() {// 问题类型
        val valueList = JuBaoData.getQuestionTypeList()

        val valueStrList = ArrayList<String>()
        for (areaBean in valueList) {
            valueStrList.add(areaBean.desc)
        }

        initSpinnerView(questionTypeSpinner, valueStrList, object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

                if (view != null) {
                    val tv = view as TextView
                    tv.setTextColor(ContextCompat.getColor(this@JuBao12380JuBaoNiMingActivity, R.color.page_content))
                    tv.paint.textSize = UIUtil.sp2px(this@JuBao12380JuBaoNiMingActivity, 14f).toFloat()
                    tv.gravity = Gravity.START
                    questionTypeStr = valueList[position].desc
                }
            }
        })
    }

    private var spinnerAdapter: ArrayAdapter<String>? = null

    private fun initSpinnerView(
        spinner: AppCompatSpinner, streetStrList: ArrayList<String>, itemSelectedListener: AdapterView.OnItemSelectedListener?
    ) {
        spinnerAdapter = ArrayAdapter(this@JuBao12380JuBaoNiMingActivity, R.layout.street_spinner_item, streetStrList!!)
        spinnerAdapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.adapter = spinnerAdapter
        spinner.dropDownVerticalOffset = 100

        spinner.onItemSelectedListener = itemSelectedListener
    }
}